﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GrupyzServices.BdAccess;
using PreBuildGrupyz.Models;


namespace PreBuildGrupyz.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        IDealRepository dealrepo;
        IparceiroRepository _ParceiroRepository;

        public HomeController()
            : this(new DealRepository(),new ParceiroRepository())
        { }


        public HomeController(IDealRepository repository, IparceiroRepository parceiroRep)
        {
            //FormsAuthentication.SignOut();
            dealrepo = repository;
            _ParceiroRepository = parceiroRep;
        }

        public ActionResult Index()
        {
            return View(dealrepo.GetDayDeal());
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult HowItWorks()
        {
            return View();
        }

        public ActionResult PoliticaPrivacidade()
        {
            return View();
        }

        public ActionResult Termos()
        {
            return View();
        }

        public ActionResult FAQ()
        {
            return View();
        }

        public ActionResult Contactos()
        {
            return View();
        }

        public ActionResult Referencia(string id)
        {
            HttpCookie cookie = new HttpCookie("Referencia", id);
            cookie.Expires = DateTime.Now.AddHours(72);
            Response.Cookies.Add(cookie);

            return RedirectToAction("Index");
        }

        public JsonResult GetVendidos(string id)
        {
            return Json(dealrepo.GetQtdVendida(int.Parse(id)).ToArray<QtdVendida>(),JsonRequestBehavior.AllowGet);
        }


        public ActionResult GetMarkers(int id)
        {
            MarkerList markers = GetMarkersObjects(id);
            return Json(markers, JsonRequestBehavior.AllowGet);
        }

        public MarkerList GetMarkersObjects(int id)
        {
            //var deal = _Dealrepository.GetDeal(id);
            DealViewModel viewmodel = dealrepo.GetDealDetalhe(id); //new DealViewModel { Negocio = deal, _Parceiro = _ParceiroRepository.GetParceiroById(deal.Id) };
            MarkerList list = new MarkerList();
            list.markers = new List<Marker>();

            foreach (var item in viewmodel.GeoLocais)
            {
                list.markers.Add(new Marker
                {
                    html = "<img src=\"" + viewmodel.Imagem + "\" width=\"50\" height=\"50\" /><br/>" + viewmodel.Descricao,
                    lat = item.Latitude,
                    lng = item.Longitude,
                    label = viewmodel.NomeParceiro
                }); 
            }

            return list;

        }
    }
}
